

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import org.json.JSONObject;

import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import java.awt.Color;
import java.awt.CardLayout;
import javax.swing.SwingConstants;



public class ClientUI extends JFrame implements Runnable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField TF_server;
	private static ClientUI frame;
	private JButton B_welcome;
	private JButton B_import;
	private JButton B_change;
	private JButton B_distribute;
	private JPanel P_main;
	private JPanel P_welcome;
	private StudentInfo P_change;
	private ImportDataPanel P_import;
	private DistributeDorPanel P_distribute;
	private CardLayout card;
	 private JLabel L_server;
	/**
	 * Launch the application.
	 */
	

	public static void main(String[] args) {
		try{
			EventQueue.invokeLater(new Runnable() {
				public void run() {
					try {
						frame = new ClientUI();
						frame.setResizable(false);
						frame.setVisible(true);
						Thread thread = new Thread(frame);  
						thread.start(); 
					} catch (Exception e) {
						JOptionPane.showMessageDialog(null, "Unfortuately System crashed \n"+"Error Info:"+e);
						e.printStackTrace();
						System.exit(0);
					}
				}
			});
		}catch(Exception e1){
			System.out.println(e1);
		}
	}

	/**
	 * Create the frame.
	 */
	public void run()
	{
		try{
			while(true)
			{
				Thread.currentThread();
				if(TF_server.getBackground()==Color.GREEN)
					Thread.sleep(1000);
				else
					Thread.sleep(5000);
				try{
					JSONObject obj = new JSONObject(new SocketOp().request("{\"method\":\"SayHi\"}"));
					if(obj.getString("method").equals("SayHi")){
						TF_server.setBackground(Color.GREEN);
					}
				}catch(Exception e1){
					TF_server.setBackground(Color.RED);
				}
			}
		}catch(Exception e){
			
		}
		
		
	}
	public ClientUI() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 558, 633);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		TF_server = new JTextField();
		TF_server.setBounds(104, 41, 22, 21);
		TF_server.setBackground(Color.RED);
		TF_server.setEditable(false);
		TF_server.setColumns(10);
		
		setLocationRelativeTo(null);
		B_welcome = new JButton("欢迎使用");
		
		B_welcome.setBounds(154, 10, 160, 38);
		B_import = new JButton("录入信息");
		
		B_import.setBounds(324, 10, 160, 38);
		B_distribute = new JButton("分配宿舍");
		
		B_distribute.setBounds(154, 58, 160, 38);
		B_change = new JButton("学生信息");
		
		B_change.setBounds(324, 58, 160, 38);
		B_welcome.setEnabled(false);
		P_welcome = new WelcomePanel();
		P_import = new ImportDataPanel();
		P_distribute = new DistributeDorPanel();
		P_change = new StudentInfo();
		
		P_main = new JPanel();
		P_main.setBounds(0, 105, 542, 489);
		
		card = new CardLayout();
		P_main.setLayout(card);
		P_main.setVisible(true);
		P_main.add("0",P_welcome);
		P_main.add("1",P_import);
		P_main.add("2",P_distribute);
		P_main.add("3",P_change);
		
		B_welcome.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				B_welcome.setEnabled(false);
				B_import.setEnabled(true);
				B_distribute.setEnabled(true);
				B_change.setEnabled(true);
				card.show(P_main, "0");
				
			}
		});
		B_import.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				B_welcome.setEnabled(true);
				B_import.setEnabled(false);
				B_distribute.setEnabled(true);
				B_change.setEnabled(true);
				P_import.P_stu.updateComboBox();
				card.show(P_main, "1");
			}
		});
		B_distribute.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				B_welcome.setEnabled(true);
				B_import.setEnabled(true);
				B_distribute.setEnabled(false);
				B_change.setEnabled(true);
				
				String sendMsg = "{\"method\":\"show_room\",\"room_sex\":\"boy\",\"room_status\":\""+(P_distribute.comboBox.getSelectedIndex()==0?"all":"avilable")+"\"}";
				P_distribute.updateData(sendMsg);
				
				
				card.show(P_main, "2");
			}
		});
		B_change.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				B_welcome.setEnabled(true);
				B_import.setEnabled(true);
				B_distribute.setEnabled(true);
				B_change.setEnabled(false);
				
				String sendMsg;
				if(P_change.comboBox.getSelectedIndex()==0)	sendMsg= "{\"method\":\"show_student\",\"student_sex\":\"all\"}";
				else if(P_change.comboBox.getSelectedIndex()==1)	sendMsg= "{\"method\":\"show_student\",\"student_sex\":\"boy\"}";
				else sendMsg= "{\"method\":\"show_student\",\"student_sex\":\"girl\"}";
				P_change.updateData(sendMsg);
				
				card.show(P_main, "3");
			}
		});
		
		
		
		L_server = new JLabel("服务器状态:");
		L_server.setHorizontalAlignment(SwingConstants.RIGHT);
		L_server.setBounds(5, 44, 89, 15);
		
		try{
			JSONObject obj = new JSONObject(new SocketOp().request("{\"method\":\"SayHi\"}"));
			if(obj.getString("method").equals("SayHi")){
				TF_server.setBackground(Color.GREEN);
			}
		}catch(Exception e){
			
			JOptionPane.showMessageDialog(null, "Server Not Run Or Port Not Open", "Error", JOptionPane.ERROR_MESSAGE);
		}
		contentPane.setLayout(null);
		contentPane.add(L_server);
		contentPane.add(TF_server);
		contentPane.add(B_welcome);
		contentPane.add(B_import);
		contentPane.add(B_distribute);
		contentPane.add(B_change);
		contentPane.add(P_main);
	}

}
